package com.wen.util;

import com.wen.bean.Tasklist;

/**
 * @Description:
 * @Author: wenxin
 * @Date: 2022/6/23 13:52
 * @Version: 1.0
 */
public class TasklistUtil {
    public static Tasklist getTaskList(String line){
        Tasklist tasklist=new Tasklist();
        tasklist.setProcessName(getProcessName(line));
        tasklist.setPid(findpid(line));
        tasklist.setSessionName(sessionName(line));
        tasklist.setSessionNum(sessionNum(line));
        tasklist.setMemory(memory(line));
        return tasklist;
    }
    /**
     * 根据PID拿到进程名称
     */
    public static String getProcessName(String line) {
        return getItemStr(line,0);
    }
    /**
     * 解析出PID
     */
    public static String findpid(String line) {
        return getItemReverseStr(line,34);
    }
    public static String sessionName(String line) {
        return getItemStr(line,35);
    }
    public static String sessionNum(String line) {
        return getItemReverseStr(line,63);
    }
    public static String memory(String line) {
        return getItemReverseStr(line,74);
    }
    //获取值
    public static String getItemStr(String line,int startNum){
        String ss = line;
        StringBuffer buffer = new StringBuffer();
        for (int i = startNum; i < ss.length(); i++) {
            if (ss.charAt(i) != ' ') {
                buffer.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return buffer.toString();
    }
    public static String getItemReverseStr(String line,int endNum){
        if(endNum>line.length()){
            endNum=line.length();
        }
        String ss = line;
        StringBuffer buffer = new StringBuffer();
        for (int i = endNum-1; i > 0; i--) {
            if (ss.charAt(i) != ' ') {
                buffer.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return buffer.reverse().toString();
    }

}
